package servlet;

import com.alibaba.fastjson.JSON;
import dao.CommunityDao;
import dao.CourierDao;
import dao.impl.CommunityDaoImpl;
import dao.impl.CourierDaoImpl;
import pojo.entity.Community;
import pojo.entity.Courier;
import util.GetPost;
import util.JDBCDruidUtil;
import util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/courierStation")
@MultipartConfig
public class CourierServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CourierDao dao = new CourierDaoImpl();
//        System.out.println(req.getContentType());
//            courierStationName: this.courierStationName,
//            : this.communityName,
//            : this.page.currentPage,
//            pageSize: this.page.pageSize,
//            startTime: this.date[0],
//            endTime: this.date[1],
        switch(action){
            case "querySearchCourierStationList":
                String courierStationName = req.getParameter("courierStationName");
                String communityName = req.getParameter("communityName");
                Map<String,Object> map = new HashMap<>();

                if(req.getParameter("pageNum") != null){
                    int pageNum = Integer.parseInt(req.getParameter("pageNum"));
                    map.put("pageNum",pageNum);
                }
                if(req.getParameter("pageSize") != null){
                    int pageSize = Integer.parseInt(req.getParameter("pageSize"));
                    map.put("pageSize",pageSize);
                }
                String startTime = req.getParameter("startTime");
                String endTime = req.getParameter("endTime");
                map.put("courierStationName",courierStationName);
                map.put("communityName",communityName);
                map.put("startTime",startTime);
                map.put("endTime",endTime);
                List<Courier> list = dao.selectAll(map);
//                System.out.println(list);
                Map<String,Object> result = new HashMap<>();
                result.put("list",list);
                result.put("total",dao.selectCount(map));
                JDBCDruidUtil.close();
                resp.getWriter().write(JSON.toJSONString(Result.success(result)));
                break;
//                删除
            case "communityRemove":

                break;
            default:
                resp.getWriter().write(JSON.toJSONString(Result.error()));
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        String status = req.getParameter("status");
//        获取json数据
        GetPost<Courier> get = new GetPost<>();
//        返回结果
        Result res = null;
        CourierDao dao = new CourierDaoImpl();
        CommunityDao communityDao = new CommunityDaoImpl();
        Community community = new Community();
        switch(action){
            case "modifyCourierStation":
                Courier courier = get.getPost(req, Courier.class);
                boolean right;
//                System.out.println(courier);
                if(status != null){
                    right = dao.modifyStation(courier);
                }else{
                    community = communityDao.selectById(courier.getCourierStationCommunityId());
                    courier.setCourierStationCommunityName(community.getCommunityName());
//                    System.out.println("community:"+community.toString());
//                    System.out.println("addCourier:"+courier.toString());
                    right = dao.update(courier);
                }
                JDBCDruidUtil.close();
                if(right){
                    res = Result.success();
                }else{
                    res = Result.error();
                }
                resp.getWriter().write(JSON.toJSONString(res));
                break;
//                添加Courier
            case "addCourierStation":
                Courier addCourier = get.getPost(req, Courier.class);
                System.out.println(addCourier);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                addCourier.setAddTime(new Date());
                community = communityDao.selectById(addCourier.getCourierStationCommunityId());
                addCourier.setCourierStationCommunityName(community.getCommunityName());
//                System.out.println("community:"+community);
//                System.out.println("addCourier:"+addCourier);
                boolean insertRight = dao.insert(addCourier);
                JDBCDruidUtil.close();
                if(insertRight){
                    res = Result.success();
                }else{
                    res = Result.error();
                }
                resp.getWriter().write(JSON.toJSONString(res));
                break;
            default:
                resp.getWriter().write(JSON.toJSONString(Result.error()));
                break;
        }

    }
}
